package main

import (
	"fmt"
	"net/http"
	_ "net/http/pprof"
)

type Counter struct {
	n int
}

func (ctr *Counter) ServeHTTP(w http.ResponseWriter, r *http.Request) {
	ctr.n++
	fmt.Fprintf(w, "counter = %d\n", ctr.n)
}

// 使用整数作为计数器
type IntCounter int

func (ctr *IntCounter) ServeHTTP(w http.ResponseWriter, req *http.Request) {
	*ctr++
	fmt.Fprintf(w, "counter = %d\n", *ctr)
}

type Chan chan *http.Request

func (ch Chan) ServeHTTP(w http.ResponseWriter, r *http.Request) {
	ch <- r
	fmt.Fprintf(w, "notify = %#v", r)
}

func main() {
	ctr := new(Counter)
	http.Handle("/counter", ctr)

	intCtr := new(IntCounter)
	http.Handle("/intcounter", intCtr)

	ch := make(Chan)
	http.Handle("/notify", ch)
	go func() {
		for req := range ch {
			fmt.Println("Received request for:", req.URL.Path)
		}
	}()

	println("host on   http://localhost:8080")
	http.ListenAndServe("localhost:8080", nil)
}
